/*
 * httpnetworkmedium.cpp
 *
 *  Created on: Aug 28, 2011
 *      Author: anonovermind <anonovermind@gmail.com>
 */

#include "httpnetworkmedium.h"
#include "httprequest.h"

namespace Wishmaster2 {
namespace Network {

static const std::string Protocol = "http";

HttpNetworkMedium::HttpNetworkMedium() :
		m_service(),
		m_work(m_service)
{
	m_eventThread = std::shared_ptr<boost::thread>(new boost::thread(boost::bind(&boost::asio::io_service::run, &m_service)));
	m_context = HttpNetworkContextPtr(new HttpNetworkContext());
	m_resolver = new HttpCachingResolver(&m_service);
}

HttpNetworkMedium::~HttpNetworkMedium() {
	m_service.stop();
	m_eventThread->interrupt();
	m_eventThread->join();
	delete m_resolver;
}

const std::string HttpNetworkMedium::protocol() const
{
	return Protocol;
}

RequestPtr HttpNetworkMedium::createRequest(int id, const Url& url)
{
	return RequestPtr(new HttpRequest(m_service, m_resolver, id, url, m_context));
}

NetworkContextPtr HttpNetworkMedium::context() const
{
	return m_context;
}


} /* namespace Network */
} /* namespace Wishmaster2 */
